/**
 * 
 */
package com.nashuju.web.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import com.nashuju.web.entity.sw.STaskSchedule;

/**
 * @author xiaoren
 *
 */
public class CSql {
	
	 protected volatile Properties     connectProperties        = new Properties();
	 public static final char UNDERLINE='_';  
	 /**
	 * @return the connectProperties
	 */
	public Properties getConnectProperties() {
		return connectProperties;//.getProperty("jdbcUrl");
	}
	
	
	public static List  excutSql(String sql,Object obj) {
		
		List list = new ArrayList<>(); 
		
		  
		String url = "jdbc:mysql://192.168.1.44:3306/img_dist-2016?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull";  
		String username = "sys-img-root";  
		String password = "E6B4837E";  
		ResultSet rs = null ;
		Connection conn;
		try {
			conn = (Connection) DriverManager.getConnection(url, username, password);
			Statement stmt = (Statement) conn.createStatement();  
			rs = stmt.executeQuery(sql);  
			list = obtainEntityObject(rs,obj.getClass());
			stmt.close();  
			conn.close(); 
		} catch (Exception e) {
			e.printStackTrace();
		}  
		 
		return list;  
		
	}
	
	public static List obtainEntityObject(ResultSet rs, Class cls) throws Exception {  
        List list = new ArrayList<>();  
  
        Field[] fields = cls.getDeclaredFields();  
        while (rs.next()) {  
            Object obj = newInstance(cls);  
            for (int i = 0; i < fields.length; i++) {  
                Class<?> type = fields[i].getType();  
                String fieldName = fields[i].getName();  
  
               // if (type.getName().equals(String.class.getName())) { 
                	try {
                		String value = rs.getString(camelToUnderline(fields[i].getName()));  
                        setProperty(obj, fieldName, value);  
					} catch (Exception e) {
						//e.printStackTrace();
					}
                    
                //}  
            }  
            list.add(obj);  
        }  
        return list;  
  
    }  
  
    private static Object newInstance(Class<?> cls) throws Exception {  
        return cls.newInstance();  
    }  
  
    private static void setProperty(Object obj, String name, Object value) throws Exception {  
        Class<? extends Object> clazz = obj.getClass();  
        String methodName = returnSetMethodName(name);  
        Method[] ms = clazz.getMethods();  
        for (Method m : ms) {  
            if (m.getName().equals(methodName)) {  
                if (m.getParameterTypes().length == 1) {  
                    Class<?> clazzParameterType = m.getParameterTypes()[0];  
                    setFieldValue(clazzParameterType.getName(), value, m, obj);  
                    break;  
                }  
            }  
        }  
    }  
  
    private static void setFieldValue(String parameterTypeName, Object value, Method m, Object obj) throws Exception {  
        if (parameterTypeName.equals(int.class.getName())||parameterTypeName.equals(Integer.class.getName())) {  
            value = new Integer(Integer.parseInt(value.toString()));  
            m.invoke(obj, value);  
            return;  
        }  
        if (parameterTypeName.equals(String.class.getName())) {  
            m.invoke(obj, value);  
            return;  
        }  
    }  
  
    private static String returnSetMethodName(String name) { 
    	name = underlineToCamel(name);
    	
        name = name.substring(0, 1).toUpperCase() + name.substring(1, name.length());  
        return "set" + name;  
    }     
	
    public static String underlineToCamel(String param){  
        if (param==null||"".equals(param.trim())){  
            return "";  
        }  
        int len=param.length();  
        StringBuilder sb=new StringBuilder(len);  
        for (int i = 0; i < len; i++) {  
            char c=param.charAt(i);  
            if (c==UNDERLINE){  
               if (++i<len){  
                   sb.append(Character.toUpperCase(param.charAt(i)));  
               }  
            }else{  
                sb.append(c);  
            }  
        }  
        return sb.toString();  
    }  
    
    public static String camelToUnderline(String param){  
        if (param==null||"".equals(param.trim())){  
            return "";  
        }  
        int len=param.length();  
        StringBuilder sb=new StringBuilder(len);  
        for (int i = 0; i < len; i++) {  
            char c=param.charAt(i);  
            if (Character.isUpperCase(c)){  
                sb.append(UNDERLINE);  
                sb.append(Character.toLowerCase(c));  
            }else{  
                sb.append(c);  
            }  
        }  
        return sb.toString();  
    }  
   

}
